/**
 * 
 */
package my.avoskagae.client.view.gwt;

import java.util.List;

import my.avoskagae.client.presenter.ICatalogListPresenter;
import my.avoskagae.client.view.ICatalogListView;
import my.avoskagae.model.entity.Product;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.mvp4g.client.view.ReverseViewInterface;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author Max
 *
 */
public class CatalogListView extends Composite implements ICatalogListView, ReverseViewInterface<ICatalogListPresenter>{

	private ICatalogListPresenter presenter;
	private static CatalogListViewUiBinder uiBinder = GWT
			.create(CatalogListViewUiBinder.class);

	interface CatalogListViewUiBinder extends UiBinder<Widget, CatalogListView> {
	}

	@UiField(provided=true) 
	CellTable<Product> cellTable = new CellTable<Product>();
	@UiField 
	TextBox newProductName;

	@UiHandler("newProductName")
	void handlerEnter(KeyPressEvent e){
		if (e.getUnicodeCharCode()==KeyCodes.KEY_ENTER) {
			presenter.createProduct(newProductName.getText());
			newProductName.setText("");
		}
	}
	
	SingleSelectionModel<Product> selectionModel = new SingleSelectionModel<Product>();
	
	public CatalogListView() {
		initWidget(uiBinder.createAndBindUi(this));
		
		cellTable.setEmptyTableWidget(new Label("Каталог пуст."));

		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				presenter.changeSelectionProduct();
			}
		});
		cellTable.setSelectionModel(selectionModel);

		//TextColumn
		TextColumn<Product> productName = new TextColumn<Product>() {

			@Override
			public String getValue(Product object) {
				return object.getName();
			}
		};
		cellTable.addColumn(productName);
		
		//EditText
		EditTextCell cellProductName1 = new EditTextCell();
		Column<Product, String> columnProductName1 = new Column<Product, String>(cellProductName1) {
			
			@Override
			public String getValue(Product object) {
				return object.getName();
			}
		}; 
		columnProductName1.setFieldUpdater(new FieldUpdater<Product, String>() {
			
			@Override
			public void update(int index, Product object, String value) {
				presenter.renameProduct(object, value);
			}
		});
		cellTable.addColumn(columnProductName1);
	
		//DeleteButton
		ActionCell<Product> cellActionDelete = new ActionCell<Product>("X", new ActionCell.Delegate<Product>() {

			@Override
			public void execute(Product object) {
				//Подтверждение выключено?
				presenter.deleteProduct(object);
//				if (Window.confirm("Удалить "+object.getName()+"?")) {
//					presenter.deleteProduct(object);
////					Window.alert(object.getName()+" Удален");
//				} else {
////					Window.alert(object.getName()+" НЕ Удален");
//				}
			}
		});
		Column<Product, Product> columnActionDelete = new Column<Product, Product>(cellActionDelete) {

			@Override
			public Product getValue(Product object) {
				return object;
			}
		};
		cellTable.addColumn(columnActionDelete);
		
		ButtonCell cellButton = new ButtonCell();
		Column<Product, String> collButton = new Column<Product, String>(cellButton) {
			
			@Override
			public String getValue(Product object) {
				return object.getNeedPurchase()?"V":"O";
			}
		};
		collButton.setFieldUpdater(new FieldUpdater<Product, String>() {
			
			@Override
			public void update(int index, Product object, String value) {
				presenter.switchNeedProduct(object);
			}
		});
		cellTable.addColumn(collButton);
		
	}
	

	
	
	
	@Override
	public void setPresenter(ICatalogListPresenter presenter) {
		this.presenter = presenter;		
	}

	@Override
	public ICatalogListPresenter getPresenter() {
		return this.presenter;
	}

	@Override
	public void refreshData(List<Product> listProduct) {
		cellTable.setRowData(listProduct);
	}

	@Override
	public IsWidget getWidgetItself() {
		return this;
	}
	
	@Override
	public Product getSelectionProduct(){
		return selectionModel.getSelectedObject();
	}
	
	@Override
	public void setSelectionProduct(Product product){
		selectionModel.setSelected(product, true);
	}
	
	@Override
	public void resetSelectionProduct(Product product){
		selectionModel.setSelected(product, false);
	}
	
	@Override
	public void showAlert(String msg){
		Window.alert(msg);
	}

}
